// pages/pay/pay.js
var base = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid: "",
    phonenum: "",
    totalPrice: 0,
    totalNum: 0,
    drinkList: "",
    //传回后端的drinklist
    dl: "",
    address: "",
    name: "",
    userid: ""
  },
  goWePay: function () {
      wx.showModal({
        title: '喜茶',
        content: '合计￥' + this.data.totalPrice,
        success: (result) => {
          if (result.confirm) {
            wx.request({
              url: 'http://localhost:8080/wx/user/getInfo1/' + base.globalData.openid,
              success: (result) => {
                this.setData({
                  userid: result.data.data.userid
                })
              }
            })
            this.setData({
              openid: base.globalData.openid,
              address: base.globalData.address,
              phone: base.globalData.phone,
              name: base.globalData.username
            })


            wx.request({
              data: {
                buyerName: this.data.name,
                phone: this.data.phone,
                address: this.data.address,
                weixinOpenid: this.data.openid,
                ordersAmount: this.data.totalPrice,
                userId: this.data.userid,
              },
              url: 'http://localhost:8080/wx/orders',
              method: "POST",
              success: (result) => {
                console.log(result)
                if (result.data.code == 200) { //判断条件不一定成熟
                  wx.showToast({
                    title: '下单成功！',
                    image: "/icon/success.png"
                  })


                  //获取ordersId 
                  wx.request({
                    data: {
                      weixinOpenid: base.globalData.openid
                    },
                    url: 'http://localhost:8080/wx/orders/selectnoworder',
                    success: (result) => {
                      for (var i = 0; i < this.data.drinkList.length; i++) {
                        wx.request({
                          data: {
                            ordersId: result.data.ordersId,
                            goodsUrl: this.data.drinkList[i].imgUrl,
                          },
                          url: 'http://localhost:8080/wx/ordersdetail',
                          method: "POST",
                        })
                      }
                    }
                  })

                  //清空购物车缓存
                  base.cart.clear();
                  setTimeout(function () {
                    // 回传后端订单，去往订单页面
                    wx.switchTab({
                      url: '../order/order',
                    })
                  }, 2000)
                } else
                  wx.showToast({
                    title: '下单失败！',
                    image: "/icon/fail.png"
                  })
              }
            })
          } else {
            console.log('点击取消回调')
          }
        }
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //判断是否登陆
    if (base.globalData.openid == "") {
      wx.showToast({
        title: ' 请先登录 ! ',
        image: "/icon/fail.png",
        duration: 2000,
        success: function () {
          setTimeout(function () {
            wx.reLaunch({
              url: '../login/login',
            })
          }, 1000);
        }
      })
    } else {
      if (base.globalData.phone == "" || base.globalData.address == "") {
        wx.showToast({
          title: ' 请补全相关信息 ! ',
          image: "/icon/fail.png",
          duration: 2000,
          success: function () {
            setTimeout(function () {
              wx.reLaunch({
                url: '../user/user/changeUserInfo/changeUserInfo',
              })
            }, 1000);
          }
        })
      }
      //取出全局变量中的用户数据信息
      this.setData({
        phone: base.globalData.phone,
        address: base.globalData.address
      })
      //获得商品列表  
      var l = base.cart.getList();
      var tp = 0;
      var tn = 0;
      var tmpList = [];
      for (var i = 0; i < l.length; i++) {
        l[i].imgUrl = 'http://localhost:8080/' + l[i].picUrl;
        tp += l[i].price * l[i].num;
        tn += l[i].num;
        tmpList[i] = {
          drinkId: l[i].id,
          drinkName: l[i].name,
          drinkPrice: l[i].price * l[i].num,
          drinkInfo: l[i].size,
          drinkNum: l[i].num
        }

      }
      console.log(tmpList)
      console.log(l)
      this.setData({
        totalPrice: tp,
        totalNum: tn,
        drinkList: l,
        dl: tmpList
      })
    }
  },
  changeMessage: function () {
    wx.navigateTo({
      url: '../user/user/changeUserInfo/changeUserInfo',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})